package cn.westv.scps.service.appversion.impl;

import cn.westv.scps.dao.BaseDao;
import cn.westv.scps.domain.AppVersionEntity;
import cn.westv.scps.service.appversion.IAppVersionService;
import cn.westv.scps.utils.Page;
import cn.westv.scps.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/16.
 */
@Service
public class AppVersionServiceImpl implements IAppVersionService{
    @Autowired
    @Qualifier("baseDao")
    private BaseDao dao;

    @Override
    public AppVersionEntity findLatestForceVersion(String appType) {
        String hql = "from AppVersionEntity where appType = ? and is_force = ? order by publishTime desc";
        List<AppVersionEntity> list = dao.find(hql,AppVersionEntity.class,new Object[]{appType,AppVersionEntity.IS_FORCE_TRUE});
        if(list != null && list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public AppVersionEntity getById(String id) {
        return dao.get(AppVersionEntity.class,id);
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public String save(AppVersionEntity appVersion) {
        dao.save(appVersion);
        return appVersion.getId() ;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public String update(AppVersionEntity appVersionEntity) {
        String id = appVersionEntity.getId();
        dao.update(appVersionEntity);
        return id;
    }

    @Override
    public AppVersionEntity findLatest(String appType) {
        String hql = "from AppVersionEntity where appType = ? order by publishTime desc";
        List<AppVersionEntity> list = dao.find(hql,AppVersionEntity.class,new Object[]{appType});
        if(list != null && list.size() > 0){
            return list.get(0);
        }
        return null;
    }


    @Override
    public Page<AppVersionEntity> findByPage(int page, int pageSize, AppVersionEntity appVersionEntity) {
        String hql = " from AppVersionEntity where 1= 1";
        Page<AppVersionEntity> pageBean = new Page<AppVersionEntity>();
        List<Object> params = new ArrayList<Object>();
        if(StringUtils.isNotBlank(appVersionEntity.getAppType()) ){
            hql += " and appType = ?";
            params.add(appVersionEntity.getAppType());
        }
        if(StringUtils.isNotBlank(appVersionEntity.getSubscribe())){
            hql += " and subscribe like ?";
            params.add("%"+appVersionEntity.getSubscribe()+"%");
        }
        if(StringUtils.isNotBlank(appVersionEntity.getVersionNo() )){
            hql += " and versionNo like ?";
            params.add("%"+appVersionEntity.getVersionNo()+"%");
        }
        pageBean.setPageNo(page);
        pageBean.setPageSize(pageSize);
        pageBean = dao.findPage(hql,pageBean,AppVersionEntity.class,params.toArray());
        return pageBean;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public void delete(String[] ids) {
        dao.delete(AppVersionEntity.class,ids);
    }
}
